package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加菜品以及口味
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name){
        Page<Dish> pageInfo1 = new Page<>(page,pageSize);
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name);
        wrapper.orderByDesc("update_time").orderByAsc("sort");
        dishService.page(pageInfo1,wrapper);
        //对象拷贝
        Page<DishDto> pageInfo2 = new Page<>(page,pageSize);
        ArrayList<DishDto> dishDtoList = new ArrayList<>();
        //把dish的数据挪到dishDto
        for (Dish dish : pageInfo1.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //根据id查询分类对象
            Category category = categoryService.getById(dish.getCategoryId());
            //设置菜品分类名称
            dishDto.setCategoryName(category.getName());
            //将dishDto添加到集合中
            dishDtoList.add(dishDto);
        }
        //设置pageInfo2的list集合
        pageInfo2.setRecords(dishDtoList);
        return R.success(pageInfo2);
    }
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品信息成功");
    }

    /**
     * 批量修改菜品状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R<String> Status(@PathVariable Integer status,@RequestParam List<Long>ids){
        Dish dish = new Dish();
        dish.setStatus(status);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        dishService.update(dish,wrapper);
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return R.success("更改状态成功");
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String>delete(@RequestParam List<Long>ids){
        dishService.removeWithDishFlavor(ids);
        return R.success("删除成功");
    }

    /**
     * 菜品列表查询
     * @param dish
     * @return
     */
    @GetMapping("list")
    public R<List<DishDto>> list(Dish dish){
        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper1.eq(StringUtils.isNotBlank(dish.getName()),Dish::getName,dish.getName());
        wrapper1.eq(Dish::getStatus,1);
        wrapper1.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishList = dishService.list(wrapper1);
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish item : dishList) {
            DishDto dishDto = new DishDto();
            //基本属性拷贝
            BeanUtils.copyProperties(item, dishDto);
            //根据菜品id查询对应的口味集合
            QueryWrapper<DishFlavor> Wrapper2 = new QueryWrapper<>();
            Wrapper2.eq("dish_id", item.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(Wrapper2);
            //设置菜品口味的数据
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }
        String key = "dish_" + dish.getCategoryId();
        List<DishDto> redisData = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if (redisData!=null){
            return R.success(dishDtoList);
        }
        return R.success(dishDtoList);
    }
}
